package com.mediacloud.app.newsmodule.view

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.eventTrack.DataCollectRequestButtonClickExpansion
import com.mediacloud.app.assembly.util.isFastClick
import com.mediacloud.app.assembly.util.load
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.datacollect.IButtonClickDataCollect
import com.mediacloud.app.newsmodule.utils.LoginUtils
import com.mediacloud.app.newsmodule.utils.spider.FollowKit
import com.mediacloud.app.newsmodule.utils.spider.IAttentionCall
import com.mediacloud.app.newsmodule.utils.spider.SpiderKit
import com.mediacloud.app.newsmodule.utils.spider.SpiderStatus
import com.mediacloud.app.user.model.UserInfo
import com.zimeiti.controler.AttentionEvent
import com.zimeiti.details.MediaAuthorDetailActivity
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class BaseSpiderView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr), View.OnClickListener, IAttentionCall {

    var articleItem: ArticleItem? = null
    var spiderImage: ImageView
    var spiderFollow: TextView
    var spiderAuthor: TextView
    var call: IAttentionCall? = null
    var isDestroy = false

    init {
        View.inflate(context, R.layout.base_spider_layout, this)
        spiderImage = findViewById(R.id.spiderImage)
        spiderAuthor = findViewById(R.id.spiderAuthor)
        spiderFollow = findViewById(R.id.spiderFollow)
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
    }


    fun setDate(articleItem: ArticleItem) {
        visibility = View.VISIBLE
        this.articleItem = articleItem
        val spider = articleItem.spider
        val drawable = ContextCompat.getDrawable(context, R.drawable.new_user_logo_login)
        drawable?.let { spiderImage.load(spider?.userImage, it) }
        spiderAuthor.text = spider?.userNickName
        spiderFollow.setOnClickListener(this)
        spiderAuthor.setOnClickListener(this)
        spiderImage.setOnClickListener(this)
        checkIsAttention()
    }

    fun attention() {
        spiderFollow.isClickable = false
        FollowKit.followOrNot(null,context, articleItem?.spider?.userId, articleItem?.spider?.userNickName,this,articleItem)
        val operate = if (!spiderFollow.isSelected) DataCollectRequestButtonClickExpansion.POSITIVE_ACTION else DataCollectRequestButtonClickExpansion.NEGATIVE_ACTION
        val userInfo = UserInfo.getUserInfo(context)
        if (context is IButtonClickDataCollect) {
            DataCollectRequestButtonClickExpansion.operateMeidaAttention(
                context.javaClass.name,
                articleItem?.spider?.userId,
                articleItem?.spider?.userNickName,
                operate,
                userInfo.userid,
                userInfo.nickname
            )
        } else {
            //页面的名字有点问题,先暂时这样处理吧
            DataCollectRequestButtonClickExpansion.operateMeidaAttention(
                this.javaClass.name,
                articleItem?.spider?.userId,
                articleItem?.spider?.userNickName,
                operate,
                userInfo.userid,
                userInfo.nickname
            )
        }
    }

    fun checkIsAttention() {
        if (!UserInfo.isLogin(context)) {
            spiderFollow.visibility = VISIBLE
            return
        }
        articleItem?.let {

            onAttentionResult( it.isFollowed )
        }
    }

    fun goSpiderHome() {
        val author_id = articleItem?.spider?.userId
        if (!TextUtils.isEmpty(author_id)) {
            MediaAuthorDetailActivity.start(context, author_id)
        }
    }


    override fun onClick(v: View?) {
        if (v == spiderAuthor || v == spiderImage) {
            if (isFastClick())
                goSpiderHome()
        } else if (v == spiderFollow) {
           // if (!NoDoubleClickUtils.scrollRepeatOut()) {
                if (UserInfo.isLogin(context)) {
                    attention()
                } else {
                    if (isFastClick())
                     LoginUtils.invokeLogin(context,3)
                }
          //  }

        }
    }

    override fun onAttentionResult(attention: Boolean) {
        if (isDestroy) return
        spiderFollow.visibility = VISIBLE
        spiderFollow.isClickable = true
        if (attention) {
            spiderFollow.isSelected = true
            //spiderFollow.text = "已关注"
        } else {
            spiderFollow.isSelected = false
            //spiderFollow.text = "+ 关注"
        }
        call?.onAttentionResult(attention)
    }

    override fun onAttentionFail() {
        if (isDestroy) return
        spiderFollow.isClickable = true
        call?.onAttentionFail()
    }

    /**
     * 退出详情页的时候一定要掉这个方法，否则会有内存泄露
     */
    fun destroy() {
        isDestroy = true;
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
    }

    override fun onDetachedFromWindow() {
        destroy()
        super.onDetachedFromWindow()
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onAttentionEvent(event: AttentionEvent) {
       checkIsAttention()
    }
}